package com.ruoyi.scm.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.response.BaseResult;
import com.ruoyi.scm.domain.ScmSaleSupplier;
import com.ruoyi.scm.service.IScmSaleSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 供应链SCM-销售管理-供应商Controller
 *
 *@author Ryan
 *@date 2025-03-12
 */
@Api(tags="供应链SCM-销售管理-供应商")
@RestController
@RequestMapping("/scm/supplier")
public class ScmSaleSupplierController extends BaseController
{
    @Autowired
    private IScmSaleSupplierService scmSaleSupplierService;

    /**
     * 查询供应链SCM-销售管理-供应商列表
     */
    @ApiOperation("销售管理-供应商列表")
    @GetMapping("/list")
    public TableDataInfo<List<ScmSaleSupplier>> list(ScmSaleSupplier scmSaleSupplier)
    {
        startPage();
        List<ScmSaleSupplier> list = scmSaleSupplierService.selectScmSaleSupplierList(scmSaleSupplier);
        return getDataTable(list);
    }

    /**
     * 查询供应链SCM-销售管理-供应商列表（不分页）
     */
    @ApiOperation("销售管理-供应商列表（不分页）")
    @GetMapping("/supplierList")
    public BaseResult<List<ScmSaleSupplier>> supplierList(ScmSaleSupplier scmSaleSupplier)
    {
        return BaseResult.success(scmSaleSupplierService.selectScmSaleSupplierList(scmSaleSupplier));
    }

    /**
     * 导出供应链SCM-销售管理-供应商列表
     */
    @ApiOperation("导出销售管理-供应商列表")
    @Log(title = "供应链SCM-销售管理-供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmSaleSupplier scmSaleSupplier)
    {
        List<ScmSaleSupplier> list = scmSaleSupplierService.selectScmSaleSupplierList(scmSaleSupplier);
        ExcelUtil<ScmSaleSupplier> util = new ExcelUtil<ScmSaleSupplier>(ScmSaleSupplier.class);
        util.exportExcel(response, list, "供应链SCM-销售管理-供应商数据");
    }

    /**
     * 获取供应链SCM-销售管理-供应商详细信息
     */
    @ApiOperation("销售管理-供应商详细信息")
    @GetMapping(value = "/{id}")
    public BaseResult<ScmSaleSupplier> getInfo(@PathVariable("id") Long id)
    {
        return BaseResult.success(scmSaleSupplierService.selectScmSaleSupplierById(id));
    }

    /**
     * 新增供应链SCM-销售管理-供应商
     */
    @ApiOperation("新增销售管理-供应商")
    @Log(title = "供应链SCM-销售管理-供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public BaseResult<Integer> add(@RequestBody ScmSaleSupplier scmSaleSupplier)
    {
        // 申请状态 0未提交 1待审核 2已审核
        scmSaleSupplier.setApplicantStatus(0);
        return BaseResult.success(scmSaleSupplierService.insertScmSaleSupplier(scmSaleSupplier));
    }

    /**
     * 修改供应链SCM-销售管理-供应商
     */
    @ApiOperation("修改销售管理-供应商")
    @Log(title = "供应链SCM-销售管理-供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public BaseResult<Integer> edit(@RequestBody ScmSaleSupplier scmSaleSupplier)
    {
        scmSaleSupplier.setApplicantStatus(0);
        scmSaleSupplier.setApprovedId(null);
        scmSaleSupplier.setApprovedStatus(null);
        scmSaleSupplier.setApprovedDate(null);
        scmSaleSupplier.setApprovedComments("");
        return BaseResult.success(scmSaleSupplierService.updateScmSaleSupplier(scmSaleSupplier));
    }

    @ApiOperation("提交供应商申请")
    @Log(title = "提交供应商申请", businessType = BusinessType.UPDATE)
    @PutMapping("/submit")
    public BaseResult<Integer> submit(@RequestBody ScmSaleSupplier scmSaleSupplier) {
        scmSaleSupplier.setApplicantStatus(1);
        scmSaleSupplier.setApplicantDate(DateUtils.getNowDate());
        if(scmSaleSupplier.getId() != null) {
            // 修改提交
            ScmSaleSupplier supplier = scmSaleSupplierService.selectScmSaleSupplierById(scmSaleSupplier.getId());
            if(supplier == null) {
                return BaseResult.error("此供应商不存在，不能提交！");
            }
            scmSaleSupplier.setApprovedId(null);
            scmSaleSupplier.setApprovedStatus(null);
            scmSaleSupplier.setApprovedDate(null);
            scmSaleSupplier.setApprovedComments("");
            return BaseResult.success(scmSaleSupplierService.updateScmSaleSupplier(scmSaleSupplier));
        }else {
            // 新增提交
            return BaseResult.success(scmSaleSupplierService.insertScmSaleSupplier(scmSaleSupplier));
        }
    }

    @ApiOperation("审核供应商申请")
    @Log(title = "审核供应商申请", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public BaseResult<Integer> approve(@RequestBody ScmSaleSupplier scmSaleSupplier) {
        scmSaleSupplier.setApplicantStatus(2);
        scmSaleSupplier.setApprovedDate(DateUtils.getNowDate());
        scmSaleSupplier.setApprovedId(SecurityUtils.getLoginUser().getUser().getUserId());
        return BaseResult.success(scmSaleSupplierService.updateScmSaleSupplier(scmSaleSupplier));
    }

    /**
     * 删除供应链SCM-销售管理-供应商
     */
    @ApiOperation("删除销售管理-供应商")
    @Log(title = "供应链SCM-销售管理-供应商", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public BaseResult<Integer> remove(@PathVariable Long[] ids)
    {
        return BaseResult.success(scmSaleSupplierService.deleteScmSaleSupplierByIds(ids));
    }
}
